package com.unionpay.upyzt.example;

import com.unionpay.upyzt.Upyzt;
import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.model.WithdrawalApplication;
import com.unionpay.upyzt.model.WithdrawalConfirm;
import com.unionpay.upyzt.resp.WithdrawalApplicationResp;
import com.unionpay.upyzt.resp.WithdrawalConfirmResp;
import com.unionpay.upyzt.util.StringUtils;
import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;

public class WithdrawalApplicationExample {

    public WithdrawalApplicationExample(String balanceAcctId) {
        this.balanceAcctId = balanceAcctId;
    }

    protected String balanceAcctId;

    /**
     * 运行 Demos
     *
     * @param balanceAcctId 电子账簿ID
     */
    public static void runDemos(String balanceAcctId) throws UpyztException {
        WithdrawalApplicationExample batchWithdrawalExample = new WithdrawalApplicationExample(balanceAcctId);
        // 提现申请
        WithdrawalApplicationResp withdrawalResp = batchWithdrawalExample.create();
        // 提现申请取消
        withdrawalResp = batchWithdrawalExample.cancel(withdrawalResp.getWithdrawalApplicationId());
        // 提现申请订单查询（系统订单号）
        withdrawalResp = batchWithdrawalExample.retrieveById(withdrawalResp.getWithdrawalApplicationId());
        // 提现申请订单查询（平台订单号）
        withdrawalResp = batchWithdrawalExample.retrieveByOutOrderNo(withdrawalResp.getOutOrderNo());

        // 提现确认
        WithdrawalConfirmResp withdrawalConfirmResp = batchWithdrawalExample.confirm(withdrawalResp.getOutBatchOrderNo());
        // 批量提现确认查询（平台批次订单号）
        withdrawalConfirmResp = batchWithdrawalExample.retrieveConfirmByBatchOutOrderNo(withdrawalConfirmResp.getOutBatchOrderNo());
    }

    /**
     * 提现申请
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public WithdrawalApplicationResp create() throws UpyztException {
        Map<String, Object> params = new HashMap<>();
        params.put("out_batch_order_no", Main.generateRandomOutNo());
        params.put("out_order_no", Main.generateRandomOutNo());
        params.put("sent_at", OffsetDateTime.now().toString());
        params.put("amount", 1);
        params.put("balance_acct_id", this.balanceAcctId);
        params.put("bank_acct_no", Upyzt.encryptField(StringUtils.randomNumeric(12)));
        params.put("password", Upyzt.encryptField("pa55w0rd"));
        params.put("remark", "用于 SDK 示例测试");
        params.put("bank_memo", "银行附言");

        // 以下参数绑定账户无需填写，提现到非绑定账户时必填
        params.put("bank_acct_type", "2");
        params.put("bank_branch_code", "308100005150");
        params.put("bank_code", "308584000013");
        params.put("name", Upyzt.encryptField("张三"));

        // 仅提现到个人非绑定账户时必填
        params.put("mobile_number", Upyzt.encryptField("1" + StringUtils.randomNumeric(10)));
        params.put("service_fee", 1);
        params.put("metadata", MockData.metadata());

        System.out.println("------- 批量提现 -------");
        WithdrawalApplicationResp resp = WithdrawalApplication.create(params);
        System.out.println(resp);
        return resp;
    }

    /**
     * 提现申请取消
     *
     * @param withdrawalId 提现申请id
     * @return 返回参数
     */
    private WithdrawalApplicationResp cancel(String withdrawalId) throws UpyztException {
        System.out.println("------- 提现申请取消 -------");
        WithdrawalApplicationResp resp = WithdrawalApplication.cancel(withdrawalId);
        System.out.println(resp);
        return resp;
    }

    /**
     * 提现订单查询（系统订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public WithdrawalApplicationResp retrieveById(String id) throws UpyztException {
        System.out.println("------- 批量提现订单查询（系统订单号） -------");
        WithdrawalApplicationResp resp = WithdrawalApplication.retrieveById(id);
        System.out.println(resp);
        return resp;
    }

    /**
     * 提现订单查询（平台订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public WithdrawalApplicationResp retrieveByOutOrderNo(String outOrderNo) throws UpyztException {
        System.out.println("------- 批量提现订单查询（平台订单号） -------");
        WithdrawalApplicationResp resp = WithdrawalApplication.retrieveByOutOrderNo(outOrderNo);
        System.out.println(resp);
        return resp;
    }

    /**
     * 批量提现确认
     *
     * @param outBatchOrderNo 批量提现平台批次号
     * @return 返回参数
     */
    private WithdrawalConfirmResp confirm(String outBatchOrderNo) throws UpyztException {
        System.out.println("------- 批量提现确认 -------");
        Map<String, Object> params = new HashMap<>();
        params.put("out_batch_order_no", outBatchOrderNo);
        WithdrawalConfirmResp resp = WithdrawalConfirm.confirm(params);
        System.out.println(resp);
        return resp;
    }

    /**
     * 提现确认状态查询
     *
     * @param outBatchOrderNo 批量提现平台批次号
     * @return 返回参数
     */
    private WithdrawalConfirmResp retrieveConfirmByBatchOutOrderNo(String outBatchOrderNo) throws UpyztException {
        System.out.println("------- 提现确认状态查询 -------");
        WithdrawalConfirmResp resp = WithdrawalConfirm.retrieveConfirmByBatchOutOrderNo(outBatchOrderNo);
        System.out.println(resp);
        return resp;
    }

}
